/*
 * 文件名称: AdminService.java
 * 版权信息: Copyright 2005-2013 SKY-MOBI Inc. All right reserved.
 * ----------------------------------------------------------------------------------------------
 * 修改历史:
 * ----------------------------------------------------------------------------------------------
 * 修改原因: 新增
 * 修改人员: Allen.Hu
 * 修改日期: 2013-3-30
 * 修改内容: 
 */
package com.drzui.tickets.order.service;

import java.util.Date;
import java.util.List;

import org.bigmouth.framework.dao.generator.Service;
import org.bigmouth.framework.util.DateUtils;
import org.bigmouth.framework.util.StringUtils;

import com.drzui.tickets.order.dao.OrdersInfoDao;
import com.drzui.tickets.order.entity.OrdersInfo;
import com.drzui.tickets.order.entity.OrdersInfoCondition;
import com.drzui.tickets.order.entity.OrdersInfoCondition.Criteria;

public class OrdersInfoService extends
		Service<OrdersInfo, OrdersInfoCondition, Integer, OrdersInfoDao> {
	
	private OrdersInfoDao ordersInfoDao;
	
	/**
	 * 获取订单信息 
	 * @param ordersInfo 查询条件
	 * @return  结果集
	 */
	public List<OrdersInfo> queryOrders(OrdersInfo ordersInfo){
		OrdersInfoCondition condition = getQueryParam(ordersInfo);
		List<OrdersInfo> rs = ordersInfoDao.selectByCondition(condition);
		if (rs != null && rs.size() > 0) {
			return rs;
		}
		return null;
	}
	
	/**
	 * 根据订单号查询订单信息
	 * @param orderid 订单号
	 * @return 订单信息
	 */
	public OrdersInfo getOrderByOrderid(Integer orderid) {
		OrdersInfoCondition condition = new OrdersInfoCondition();
		Criteria criteria = condition.createCriteria();
		criteria.andOrderidEqualTo(orderid);
		List<OrdersInfo> rs = ordersInfoDao.selectByCondition(condition);
		if (rs != null && rs.size() > 0) {
			return rs.get(0);
		}
		return null;
	}
	
	/**
	 * 新增订单信息
	 * @param ordersInfo 订单信息
	 */
	public void newOrder(OrdersInfo ordersInfo) {
		ordersInfoDao.insert(ordersInfo);
	}
	
	/**
	 * 自动获取订单id
	 * @return 订单id
	 */
	public Integer autoOrderid() {
		
		int year = DateUtils.getCurrentYear();
		int month = Integer.parseInt(DateUtils.getCurrentMonthFormat());
		int day = DateUtils.days(year, month);
		Date endtime = DateUtils.convertString2Date(year + "-" + month + "-"
				+ day);
		Date begintime = DateUtils.convertString2Date(DateUtils.getMonthFirstDay2String());
		
		OrdersInfo ordersInfo = new OrdersInfo();
		ordersInfo.setEndtime(endtime);
		ordersInfo.setBegintime(begintime);
		
		OrdersInfoCondition condition = new OrdersInfoCondition();
		Criteria criteria = condition.createCriteria();
		criteria.andCreatetimeBetween(begintime,endtime);
		condition.setOrderByClause("orderid");
		
		List<OrdersInfo> rs = ordersInfoDao.selectByCondition(condition);
		
		Integer orderid = 0;
		if (rs != null && rs.size() > 0) {
			orderid = rs.get(rs.size() - 1).getOrderid();
			orderid = (month * 1000) + ((orderid % 1000) + 1);
		} else {
			orderid = (month * 1000) + 1;
		}
		
		return orderid;
	}
	
	/**
	 * 修改订单信息
	 * @param ordersInfo 订单信息
	 */
	public void modifyOrder(OrdersInfo ordersInfo) {
		ordersInfoDao.updateByPrimaryKey(ordersInfo);
	}
	
	/**
	 * 获取查询条件 组成condition
	 * 
	 * @param ordersInfo 查询条件
	 * @return
	 */
	public OrdersInfoCondition getQueryParam(OrdersInfo ordersInfo) {
		OrdersInfoCondition condition = new OrdersInfoCondition();
		Criteria criteria = condition.createCriteria();

		if (ordersInfo.getOrderid() != null && ordersInfo.getOrderid() != 0) {
			criteria.andOrderidEqualTo(ordersInfo.getOrderid());
		}
		if (!StringUtils.isEmpty(ordersInfo.getCustomername())) {
			criteria.andCustomernameLike("%" + ordersInfo.getCustomername() + "%");
		}
		if (!StringUtils.isEmpty(ordersInfo.getCustomeraddress())) {
			criteria.andCustomeraddressLike("%" + ordersInfo.getCustomeraddress() + "%");
		}
		if (!StringUtils.isEmpty(ordersInfo.getCustomerphone())) {
			criteria.andCustomerphoneLike("%" + ordersInfo.getCustomerphone() + "%");
		}
		if (ordersInfo.getBegintime() != null && ordersInfo.getEndtime() != null) {
			criteria.andCreatetimeBetween(ordersInfo.getBegintime(), ordersInfo.getEndtime());
		} else if (ordersInfo.getBegintime() != null && ordersInfo.getEndtime() == null) {
			criteria.andCreatetimeGreaterThanOrEqualTo(ordersInfo.getBegintime());
		} else if (ordersInfo.getEndtime() != null && ordersInfo.getBegintime() == null){
			criteria.andCreatetimeLessThanOrEqualTo(ordersInfo.getEndtime());
		}
		if (!StringUtils.isEmpty(ordersInfo.getOrderbyclause())) {
			condition.setOrderByClause(ordersInfo.getOrderbyclause());
		}
		
		return condition;
	}
	
	@Override
	protected OrdersInfoDao getDao() {
		return ordersInfoDao;
	}
	
	public OrdersInfoDao getOrdersInfoDao() {
		return ordersInfoDao;
	}

	public void setOrdersInfoDao(OrdersInfoDao ordersInfoDao) {
		this.ordersInfoDao = ordersInfoDao;
	}
}
